package Principal;

import java.awt.print.PrinterException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author pato
 */
public class cierreBasico extends javax.swing.JDialog {

    /**
     * Creates new form cierreBasico
     */
    PreparedStatement ps;
    ResultSet rs;
    float fdevoluciones = 0.0f;
    float fgastos = 0.0f;
    float fventas = 0.0f;
    float fcaja = 0.0f;
    float fabonos = 0.0f;
    float enCaja = 0.0f;
    float billetes = 0.0f;
    float monedas = 0.0f;
    float ftotal = 0.0f;
    int ib1000 = 0, ib500 = 0, ib200 = 0, ib100 = 0, ib50 = 0, ib20 = 0, im10 = 0, im5 = 0, im2 = 0, im1 = 0, ic50 = 0, ic20 = 0;
    NumberFormat formatter;

    public cierreBasico(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        b1000.setDocument(new LimitadorCaracteres());
        b500.setDocument(new LimitadorCaracteres());
        b200.setDocument(new LimitadorCaracteres());
        b100.setDocument(new LimitadorCaracteres());
        b50.setDocument(new LimitadorCaracteres());
        b20.setDocument(new LimitadorCaracteres());
        m10.setDocument(new LimitadorCaracteres());
        m5.setDocument(new LimitadorCaracteres());
        m2.setDocument(new LimitadorCaracteres());
        m1.setDocument(new LimitadorCaracteres());
        c50.setDocument(new LimitadorCaracteres());
        c20.setDocument(new LimitadorCaracteres());
        formatter = NumberFormat.getCurrencyInstance();
        getRootPane().setDefaultButton(jButton1);
        try {
            Connection cn = local_connection.getconnect();
            ps = cn.prepareStatement("select dinero from devoluciones where fecha > (select inicio from sesion where id = ?) and fecha < current_timestamp and sucursal = ? and caja = ?");
            ps.setInt(1, VentanaPrincipal.sesionId);
            ps.setInt(2, VentanaPrincipal.sucursal);
            ps.setInt(3, VentanaPrincipal.caja);
            rs = ps.executeQuery();
            if(rs.next())
            if (rs.getObject(1) != null) {
                fdevoluciones = rs.getFloat(1);
                devoluciones.setText(formatter.format(fdevoluciones));
            }
            ps = cn.prepareStatement("select sum((preciovendido*cantidad)-if(credito is null,0.0,credito-restante)) from detalleventa join venta on detalleventa.folio = venta.folio left outer join credito on credito.folio = venta.folio join productos on detalleventa.id_producto = productos.id_producto where venta.fecha > (select inicio from sesion where id = ?) and venta.fecha < current_timestamp and venta.tipopago = 'Efectivo' and productos.tipo = 'Producto' and sucursal = ? and caja = ? and venta.activo = 1");
            ps.setInt(1, VentanaPrincipal.sesionId);
            ps.setInt(2, VentanaPrincipal.sucursal);
            ps.setInt(3, VentanaPrincipal.caja);
            rs = ps.executeQuery();
            if(rs.next())
            if (rs.getObject(1) != null) {
                fventas = rs.getFloat(1);
                ventas.setText(formatter.format(fventas));
            }
            ps = cn.prepareStatement("select sum(cantidad) from gastos where gastos.fecha > (select inicio from sesion where id = ?) and gastos.fecha < current_timestamp and sucursal = ? and caja = ?");
            ps.setInt(1, VentanaPrincipal.sesionId);
            ps.setInt(2, VentanaPrincipal.sucursal);
            ps.setInt(3, VentanaPrincipal.caja);
            rs = ps.executeQuery();
            if(rs.next())
            if (rs.getObject(1) != null) {
                fgastos = rs.getFloat(1);
                gastos.setText(formatter.format(fgastos));
            }
            ps = cn.prepareStatement("select sum(acuenta) from abono where abono.fecha > (select inicio from sesion where id = ?) and abono.tipopago = 'Efectivo' and abono.fecha < current_timestamp");
            ps.setInt(1, VentanaPrincipal.sesionId);
            rs = ps.executeQuery();
            if(rs.next())
            if (rs.getObject(1) != null) {
                fabonos = rs.getFloat(1);
                abonos.setText(formatter.format(fabonos));
            }
            ps = cn.prepareStatement("select iniciocaja from sesion where id = ?");
            ps.setInt(1, VentanaPrincipal.sesionId);
            rs=ps.executeQuery();
            if(rs.next())
            if (rs.getObject(1) != null) {
                fcaja = rs.getFloat(1);
                caja.setText(formatter.format(fcaja));
            }
            ftotal = fabonos - fgastos + fventas - fdevoluciones + fcaja;
            total.setText(formatter.format(ftotal));
        } catch (SQLException ex) {
            VentanaPrincipal.logger.log(Level.SEVERE, null, ex);
        }
actualizarEtiquetas();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        devoluciones = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        ventas = new javax.swing.JLabel();
        gastos = new javax.swing.JLabel();
        caja = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        abonos = new javax.swing.JLabel();
        total = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        b1000 = new javax.swing.JTextField();
        b500 = new javax.swing.JTextField();
        b200 = new javax.swing.JTextField();
        b100 = new javax.swing.JTextField();
        b50 = new javax.swing.JTextField();
        b20 = new javax.swing.JTextField();
        jLabel14 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        jLabel17 = new javax.swing.JLabel();
        jLabel18 = new javax.swing.JLabel();
        jLabel19 = new javax.swing.JLabel();
        jLabel20 = new javax.swing.JLabel();
        m10 = new javax.swing.JTextField();
        m5 = new javax.swing.JTextField();
        m2 = new javax.swing.JTextField();
        m1 = new javax.swing.JTextField();
        c50 = new javax.swing.JTextField();
        c20 = new javax.swing.JTextField();
        jLabel21 = new javax.swing.JLabel();
        jLabel22 = new javax.swing.JLabel();
        lblBilletes = new javax.swing.JLabel();
        lblMonedas = new javax.swing.JLabel();
        jLabel23 = new javax.swing.JLabel();
        lblenCaja = new javax.swing.JLabel();
        jLabel24 = new javax.swing.JLabel();
        lblDiferencia = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Cierre de Caja");
        setResizable(false);

        jLabel1.setText("Total:");

        jLabel2.setText("Devoluciones:$");

        jLabel3.setText("Ventas:$");

        jLabel4.setText("Gastos:$");

        jLabel5.setText("Caja:$");

        devoluciones.setText("0.00");

        jButton1.setText("Ok");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        ventas.setText("0.00");

        gastos.setText("0.00");

        caja.setText("0.00");

        jLabel6.setText("  Abonos:$");

        abonos.setText("0.00");

        total.setText("jLabel7");

        jLabel7.setText("Billetes");

        jLabel8.setText("$1,000.00:");

        jLabel9.setText("$500.00:");

        jLabel10.setText("$200.00:");

        jLabel11.setText("$100.00:");

        jLabel12.setText("$50.00:");

        jLabel13.setText("$20.00:");

        b1000.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                b1000KeyReleased(evt);
            }
        });

        b500.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                b500KeyReleased(evt);
            }
        });

        b200.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                b200KeyReleased(evt);
            }
        });

        b100.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                b100KeyReleased(evt);
            }
        });

        b50.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                b50KeyReleased(evt);
            }
        });

        b20.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                b20KeyReleased(evt);
            }
        });

        jLabel14.setText("Monedas:");

        jLabel15.setText("$10.00:");

        jLabel16.setText("$5.00:");

        jLabel17.setText("$2.00:");

        jLabel18.setText("$1.00:");

        jLabel19.setText("$0.50:");

        jLabel20.setText("$0.20:");

        m10.setPreferredSize(new java.awt.Dimension(37, 20));
        m10.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                m10KeyReleased(evt);
            }
        });

        m5.setPreferredSize(new java.awt.Dimension(37, 20));
        m5.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                m5KeyReleased(evt);
            }
        });

        m2.setPreferredSize(new java.awt.Dimension(37, 20));
        m2.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                m2KeyReleased(evt);
            }
        });

        m1.setPreferredSize(new java.awt.Dimension(37, 20));
        m1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                m1KeyReleased(evt);
            }
        });

        c50.setPreferredSize(new java.awt.Dimension(37, 20));
        c50.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                c50KeyReleased(evt);
            }
        });

        c20.setPreferredSize(new java.awt.Dimension(37, 20));
        c20.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                c20KeyReleased(evt);
            }
        });

        jLabel21.setText("Billetes:");

        jLabel22.setText("Monedas:");

        lblBilletes.setText("jLabel23");

        lblMonedas.setText("jLabel23");

        jLabel23.setText("En Caja:");

        lblenCaja.setText("jLabel24");

        jLabel24.setText("Diferencia:");

        lblDiferencia.setText("jLabel25");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jLabel7)
                .addGap(67, 67, 67)
                .addComponent(jLabel14)
                .addGap(28, 28, 28))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(ventas)
                            .addComponent(abonos)
                            .addComponent(caja))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 32, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel21, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel8, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel9, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel10, javax.swing.GroupLayout.Alignment.TRAILING)))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(gastos)
                                    .addComponent(devoluciones))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel13, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel11, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel12, javax.swing.GroupLayout.Alignment.TRAILING)))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel23, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel24, javax.swing.GroupLayout.Alignment.TRAILING))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(total)
                                    .addComponent(lblenCaja)
                                    .addComponent(lblDiferencia))
                                .addGap(0, 0, Short.MAX_VALUE)))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(b1000, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(b500, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(b200, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(b100, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(b50, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(b20, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel17, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel16, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel15, javax.swing.GroupLayout.Alignment.TRAILING))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(m10, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(m5, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(m2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel20, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel19, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel18, javax.swing.GroupLayout.Alignment.TRAILING))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(c50, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(m1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(c20, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblBilletes)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel22)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(lblMonedas)))))
                .addContainerGap(23, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {b100, b1000, b20, b200, b50, b500});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jLabel14))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(caja)
                    .addComponent(b1000, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel15)
                    .addComponent(m10, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(ventas)
                    .addComponent(b500, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel16)
                    .addComponent(m5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(abonos)
                    .addComponent(b200, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel17)
                    .addComponent(m2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(gastos)
                    .addComponent(jLabel11)
                    .addComponent(b100, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel18)
                    .addComponent(m1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(devoluciones)
                    .addComponent(jLabel12)
                    .addComponent(b50, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel19)
                    .addComponent(c50, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(total)
                    .addComponent(jLabel13)
                    .addComponent(b20, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel20)
                    .addComponent(c20, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel21)
                    .addComponent(jLabel22)
                    .addComponent(lblBilletes)
                    .addComponent(lblMonedas)
                    .addComponent(jLabel23)
                    .addComponent(lblenCaja))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel24)
                    .addComponent(lblDiferencia)
                    .addComponent(jButton1))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {b100, b20, b200, b50, b500});

        pack();
    }// </editor-fold>//GEN-END:initComponents
class LimitadorCaracteres extends PlainDocument {

        /**
         * Método al que llama el editor cada vez que se intenta insertar
         * caracteres. Sólo debemos verificar arg1, que es la cadena que se
         * quiere insertar en el JTextField
         */
        @Override
        public void insertString(int arg0, String arg1, AttributeSet arg2) throws BadLocationException {
            for (int i = 0; i < arg1.length(); i++) // si no es digit, volvemos
            {
                if (!Character.isDigit(arg1.charAt(i))) {
                    return;
                }
            }

            // Si todos son digit, insertamos el texto en el JTextField
            super.insertString(arg0, arg1, arg2);
        }
    }
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
new TicketCorte();
        try {
                 SimpleDateFormat fecha = new SimpleDateFormat("dd/MM/yyyy");
        SimpleDateFormat hora = new SimpleDateFormat("hh:mm:ss aa");
        Date date = new Date();
        TicketCorte.fechafinal="" + fecha.format(date) + " " + hora.format(date);
            ps = local_connection.cn.prepareStatement("update sesion set b1000=?, b500 = ?, b200 = ?, b100 = ?, b50=?, b20 =?,"
                    + "m10=?, m5=?, m2=?, m1=?, c50=?, c20=?, en_caja=?, total=? where id= ?");
            ps.setInt(15,VentanaPrincipal.sesionId);
            ps.setInt(1,ib1000);
            ps.setInt(2,ib500);
            ps.setInt(3,ib200);
            ps.setInt(4,ib100);
            ps.setInt(5,ib50);
            ps.setInt(6,ib20);
            ps.setInt(7,im10);
            ps.setInt(8,im5);
            ps.setInt(9,im2);
            ps.setInt(10,im1);
            ps.setInt(11,ic50);
            ps.setInt(12,ic20);
            ps.setFloat(13, enCaja);
            ps.setFloat(14, ftotal);
            ps.execute();
            ps=local_connection.cn.prepareStatement("select concat(nombre,' ',paterno,' ',materno) as name,inicio,DATE_FORMAT(inicio, '%r') as hora from sesion join personas on personas.id_persona = sesion.idusuario where id=?");
            ps.setInt(1,VentanaPrincipal.sesionId);
            rs=ps.executeQuery();
            rs.next();
            TicketCorte.nombre=rs.getString("name");
           TicketCorte.enCaja=enCaja;
           TicketCorte.total=ftotal;
           TicketCorte.fecha="" + fecha.format(rs.getDate("inicio")) + " " + rs.getString("hora");
           TicketCorte.AddItem("Caja");
           TicketCorte.AddCantidad(formatter.format(fcaja));
           TicketCorte.AddItem("Ventas");TicketCorte.AddCantidad(formatter.format(fventas));
           TicketCorte.AddItem("Abonos");TicketCorte.AddCantidad(formatter.format(fabonos));
           TicketCorte.AddItem("Gastos");TicketCorte.AddCantidad("-"+formatter.format(fgastos));
           TicketCorte.AddItem("Devoluciones");TicketCorte.AddCantidad("-"+formatter.format(fdevoluciones));
            if(VentanaPrincipal.imprimir)
            {
            TicketCorte.job.print();
           // TicketCorte.job.print();
       // System.out.println("uno");
          //  TicketCorte.subCabezaLineas.clear();
            TicketCorte.nombre="";
            TicketCorte.impreso = false;
            TicketCorte.total=0.0f;
            TicketCorte.enCaja=0.0f;
            TicketCorte.cantidades.clear();
            TicketCorte.CabezaLineas.clear();
            TicketCorte.items.clear();
            }
                       this.dispose();
        } catch (SQLException ex) {
            VentanaPrincipal.logger.log(Level.SEVERE, null, ex);
        } catch (PrinterException ex) {
            Logger.getLogger(cierreBasico.class.getName()).log(Level.SEVERE, null, ex);
        }
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed
    void actualizarEtiquetas() {
        billetes = ib1000 * 1000 + ib500 * 500 + ib200 * 200 + ib100 * 100 + ib50 * 50 + ib20 * 20;
        monedas = im10 * 10 + im5 * 5 + im2 * 2 + im1 + ic50 * 0.5f + ic20 * 0.2f;
        enCaja = billetes + monedas;
        lblenCaja.setText(formatter.format(enCaja));
        lblBilletes.setText(formatter.format(billetes));
        lblMonedas.setText(formatter.format(monedas));
        lblDiferencia.setText(formatter.format(enCaja - ftotal));
    }
    private void b1000KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_b1000KeyReleased
        if(b1000.getText().equals(""))
            ib1000=0;
        else
        ib1000 = Integer.parseInt(b1000.getText());
        actualizarEtiquetas();
        // TODO add your handling code here:
    }//GEN-LAST:event_b1000KeyReleased

    private void b500KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_b500KeyReleased
        if(b500.getText().equals(""))
            ib500=0;
        else
        ib500 = Integer.parseInt(b500.getText());
        actualizarEtiquetas();
        // TODO add your handling code here:
    }//GEN-LAST:event_b500KeyReleased

    private void b200KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_b200KeyReleased
        if(b200.getText().equals(""))
            ib200=0;
        else
        ib200 = Integer.parseInt(b200.getText());
        actualizarEtiquetas();
        // TODO add your handling code here:
    }//GEN-LAST:event_b200KeyReleased

    private void b100KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_b100KeyReleased
        if(b100.getText().equals(""))
            ib100=0;
        else
        ib100 = Integer.parseInt(b100.getText());
        actualizarEtiquetas();
        // TODO add your handling code here:
    }//GEN-LAST:event_b100KeyReleased

    private void b50KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_b50KeyReleased
        if(b50.getText().equals(""))
            ib50=0;
        else
        ib50 = Integer.parseInt(b50.getText());
        actualizarEtiquetas();
        // TODO add your handling code here:
    }//GEN-LAST:event_b50KeyReleased

    private void b20KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_b20KeyReleased
        if(b20.getText().equals(""))
            ib20=0;
        else
        ib20 = Integer.parseInt(b20.getText());
        actualizarEtiquetas();
        // TODO add your handling code here:
    }//GEN-LAST:event_b20KeyReleased

    private void m10KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_m10KeyReleased
        if(m10.getText().equals(""))
            im10=0;
        else
        im10 = Integer.parseInt(m10.getText());
        actualizarEtiquetas();
        // TODO add your handling code here:
    }//GEN-LAST:event_m10KeyReleased

    private void m5KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_m5KeyReleased
        if(m5.getText().equals(""))
            im5=0;
        else
        im5 = Integer.parseInt(m5.getText());
        actualizarEtiquetas();
        // TODO add your handling code here:
    }//GEN-LAST:event_m5KeyReleased

    private void m2KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_m2KeyReleased
        if(m2.getText().equals(""))
            im2=0;
        else
        im2 = Integer.parseInt(m2.getText());
        actualizarEtiquetas();
        // TODO add your handling code here:
    }//GEN-LAST:event_m2KeyReleased

    private void m1KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_m1KeyReleased
        if(m1.getText().equals(""))
            im1=0;
        else
        im1 = Integer.parseInt(m1.getText());
        actualizarEtiquetas();
        // TODO add your handling code here:
    }//GEN-LAST:event_m1KeyReleased

    private void c50KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_c50KeyReleased
        if(c50.getText().equals(""))
            ic50=0;
        else
        ic50 = Integer.parseInt(c50.getText());
        actualizarEtiquetas();
        // TODO add your handling code here:
    }//GEN-LAST:event_c50KeyReleased

    private void c20KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_c20KeyReleased
        if(c20.getText().equals(""))
            ic20=0;
        else
        ic20 = Integer.parseInt(c20.getText());
        actualizarEtiquetas();
        // TODO add your handling code here:
    }//GEN-LAST:event_c20KeyReleased


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel abonos;
    private javax.swing.JTextField b100;
    private javax.swing.JTextField b1000;
    private javax.swing.JTextField b20;
    private javax.swing.JTextField b200;
    private javax.swing.JTextField b50;
    private javax.swing.JTextField b500;
    private javax.swing.JTextField c20;
    private javax.swing.JTextField c50;
    private javax.swing.JLabel caja;
    private javax.swing.JLabel devoluciones;
    private javax.swing.JLabel gastos;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel22;
    private javax.swing.JLabel jLabel23;
    private javax.swing.JLabel jLabel24;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JLabel lblBilletes;
    private javax.swing.JLabel lblDiferencia;
    private javax.swing.JLabel lblMonedas;
    private javax.swing.JLabel lblenCaja;
    private javax.swing.JTextField m1;
    private javax.swing.JTextField m10;
    private javax.swing.JTextField m2;
    private javax.swing.JTextField m5;
    private javax.swing.JLabel total;
    private javax.swing.JLabel ventas;
    // End of variables declaration//GEN-END:variables
}
